package org.pjlib.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.jaeger.library.StatusBarUtil;
import com.meiyou.jet.process.Jet;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.pjlib.BaseApp;
import org.pjlib.Event;
import org.pjlib.Logs;
import org.pjlib.common.UMengUtils;
import org.pjlib.net.HttpUtils;
import org.pjlib.net.JsonCallBack;
import org.pjlib.net.MyException;
import org.pjlib.net.RequestCall;

/**
 * Created by ws on 2019/02/21.
 *
 * @TODO 1.仅需在getContentId添加页面布局，用init()初始化
 */
public class BaseActivity extends AppCompatActivity implements UIBase, JsonCallBack {
    protected BaseApp app;
    protected Activity act;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onCreate();
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        Logs.i("Base", this.getClass().getSimpleName() + ":onCreate");
        if (getApplication() instanceof BaseApp) {
            app = (BaseApp) getApplication();
            app.addActivity(this);
        }
        act = this;
        if (getContentId() != -1) {
            setContentView(getContentId());
            Jet.bind(this);
        }
        EventBus.getDefault().register(this);
        init();
    }

    @Subscribe
    public void onEvent(Event.Updata event) {
        updata();
    }

    @Override
    public int getContentId() {
        return -1;
    }


    @Override
    public void init() {
    }

    protected void onCreate() {

    }

    @Override
    public Object getNetTag() {
        return this;
    }

    @Override
    public void updata() {

    }

    @Override
    public void onBeforeRequest(RequestCall call) {

    }

    @Override
    public void onResponseError(Throwable t, RequestCall call) {
        ToastUtils.showShort(MyException.NO_AVAILABLE_NETWORK);
    }

    @Override
    public void onResponseSuccess(RequestCall call) {

    }

    @Override
    public void onFinishRequest(RequestCall call) {

    }

    @Override
    public void toActivity(Class<? extends BaseActivity> c) {
        ActivityUtils.startActivity(c);
    }

    @Override
    public void toActivity(Class<? extends BaseActivity> c, Bundle bundle) {
        Intent intent = new Intent(this, c);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void toActivity(Intent intent) {
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (app != null) {
            app.removeActivity(this);
        }
        EventBus.getDefault().unregister(this);
        HttpUtils.cancelTag(this);
        Logs.i("BaseActivity", this.getClass().getSimpleName() + ":onDestroy");
    }

    @Override
    protected void onResume() {
        super.onResume();
        UMengUtils.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        UMengUtils.onPause(this);
    }

}
